﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using common;
using System.Windows.Forms;

namespace HTF_HMI
{
    public partial class Plan_Manager : Form
    {
        AutoSizeFormClass asc = new AutoSizeFormClass();
        HTF_DAL HtfData = new HTF_DAL();
        QueryArgs arg = new QueryArgs();

        public Plan_Manager()
        {
            InitializeComponent();
        }

        private void Plan_Manager_Load(object sender, EventArgs e)
        {
            
            
            asc.controlAutoSize(this);           
            bindingSource1.DataSource = DBClass.DBHelper.GetPlan();

        }

        public void btn_refresh_Click(object sender, EventArgs e)
        {           
            bindingSource1.DataSource = DBClass.DBHelper.GetPlan();
        }

        private void btn_edit_Click(object sender, EventArgs e)
        {           
      
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {   
            int[] rows = this.gridView1.GetSelectedRows();
            if (rows.Count() > 0)
            {
                List<int> ids = new List<int>();
                foreach (int row in rows)
                {
                    string strId = this.gridView1.GetRowCellValue(row, gridView1.Columns["ID"]).ToString();

                    DataTable dt = HtfData.GetPlanBy("ID="+ strId);
                    string strSeq = this.gridView1.GetRowCellValue(row, gridView1.Columns["BilletSeq"]).ToString();
                    string strStatus = dt.Rows[0]["PlanStatus"].ToString();// ((DataRowView)bindingSource1.Current).Row["PlanStatus"].ToString(); //避免画面刷新不及时取状态数据错误
                    if (strStatus == "1")
                    {
                        MessageBox.Show("入炉顺序号" + strSeq + "计划已入炉，不允许删除！");
                        return;
                    }
                    ids.Add(Int32.Parse( strId));
                }               
                
            }
        }

        private void buttonPassPlan_Click(object sender, EventArgs e)
        {
            if (bindingSource1.Current == null)
                return;
           
            string strPassId = ((DataRowView)bindingSource1.Current).Row["id"].ToString();

            if (Convert.ToInt32(strPassId) <= 0)
            {
                MessageBox.Show("请点选要匹配的作业计划");
                return;
            }

            DialogResult drPass = MessageBox.Show("请确认操作，会强制完成" + strPassId + "之前的计划信息！", "核对提示", MessageBoxButtons.OKCancel);
            if (drPass == DialogResult.OK)
            {
                string strSql = "update plan_tb set is_completed = 4 WHERE is_completed = 0 and id < '"
                                + strPassId + "'";

                if (HtfData.ExecuteNonQuery(strSql) > 0)
                {
                    MessageBox.Show("直接匹配计划成功");
                }
                else
                {
                    MessageBox.Show("匹配失败！");
                }
            }            
            bindingSource1.DataSource = DBClass.DBHelper.GetPlan();
        }

        private void btn_stop_Click(object sender, EventArgs e)
        {
           
        }

        private void btn_front_Click(object sender, EventArgs e)
        {
            AdjustPlanSeq("front");
        }
      
        private void btn_up_Click(object sender, EventArgs e)
        {
            AdjustPlanSeq("up");
        }

        private void btn_down_Click(object sender, EventArgs e)
        {
            AdjustPlanSeq("down");
        }

        private void btn_behind_Click(object sender, EventArgs e)
        {
            AdjustPlanSeq("behind");
        }

        private void AdjustPlanSeq(string oparate)
        {           
            int index = gridView1.GetFocusedDataSourceRowIndex();           
            string strId = this.gridView1.GetRowCellValue(index, gridView1.Columns["ID"]).ToString();
           

        }
      

        private void cms_control_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            foreach (ToolStripItem items in cms_control.Items)
            {
                if (items.Selected == true)
                {
                    switch (items.Name)
                    {
                        case "cms_top":
                            AdjustPlanSeq("front");
                            break;
                        case "cms_buttom":
                            AdjustPlanSeq("behind");
                            break;
                        case "cms_up":
                            AdjustPlanSeq("up");
                            break;
                        case "cms_down":
                            AdjustPlanSeq("down");
                            break;

                    }
                    break;
                }
            }           
        }
    }
}
